package com.fingard.dsp.bank.directbank.czb02;

import com.fingard.dsp.bank.directConfig.ActSetItem;
import com.fingard.dsp.bank.directbank.bankInterface.IDeContract;
import com.fingard.dsp.bank.reqrespobj.contract.ContractState;
import com.fingard.dsp.bank.reqrespobj.decontract.ReqDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.RetAtsDeContract;
import com.fingard.dsp.bank.reqrespobj.decontract.detail.RetAtsDeContractDetail;
import com.fingard.text.StringHelper;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

/**
 * 浙商银行代收协议签约
 * <p>
 * 参考文档：浙商银行收付通接口规范（代收）.doc
 *
 * @author FanH
 * @date 2019/9/12
 */
public class CZB02DeContract extends CZB02Base implements IDeContract {

    /**
     * 开发接口：
     * 1. 3.3.3.3	代收协议解约[0003]
     */
    @Override
    public RetAtsDeContract deContract(ReqDeContract p_req) throws Exception {
        RetAtsDeContract retAts = new RetAtsDeContract(p_req.reqHeader);
        RetAtsDeContractDetail tmpRetDetail = new RetAtsDeContractDetail();
        tmpRetDetail.reqSeqID = p_req.contractDetail.askNumber;

        try {
            ActSetItem actItem = getBankConfig().getActSetItem(p_req.contractDetail.recAct, "contract");
            if (actItem != null) {
                CZB02Header bankHeader = new CZB02Header();
                bankHeader.transCode = "0003";
                bankHeader.reqSeqNo = p_req.contractDetail.askNumber;

                XmlTextWriter xmlWriter = new XmlTextWriter();
                StringBuilder reqMsg = new StringBuilder();

                writeCommonHeader(xmlWriter, actItem, bankHeader);
                xmlWriter.writeStartElement("body");
                xmlWriter.writeElementString("xyh" , p_req.contractDetail.payAct);//协议号
                reqMsg.append("<xyh>" + p_req.contractDetail.payAct + "</xyh>");//协议号
                xmlWriter.writeEndDocument();

                String signMsg = signData(reqMsg.toString(), actItem);
                WriteBankLogLn2("签名原始数据:\r\n" + reqMsg.toString());

                String reqData = xmlWriter.toXmlString().replaceAll("<bz1 />", "<bz1>" + signMsg + "</bz1>");

                String[] tmpRetStr = sendToBank(reqData);

                if(tmpRetStr[0].length() == 0) {
                    WriteBankLogStep3(tmpRetStr[1]);

                    StringBuilder tmpSbMsg = new StringBuilder();
                    XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpRetStr[1], tmpSbMsg);
                    if (tmpBkReader != null) {
                        tmpRetDetail.respCode = "0";
                        tmpRetDetail.respInfo = tmpBkReader.getFirstText("/response/head/fhxx");
                        String tmpRespCode = tmpBkReader.getFirstText("/response/head/fhm");
                        if (tmpRespCode != null && StringHelper.hasAnyChar(tmpRespCode)) {
                            if ("000000".equals(tmpRespCode)) {
                                tmpRetDetail.deSignState = ContractState.SUCCESS;
                            } else {
                                tmpRetDetail.deSignState = ContractState.FAIL;
                            }
                        } else {
                            tmpRetDetail.respCode = "-1";
                            tmpRetDetail.respInfo = tmpBkReader.getFirstText("/response/head/fhxx");
                        }
                    } else {
                        tmpRetDetail.respCode = "-1";
                        tmpRetDetail.respInfo = tmpSbMsg.toString();
                    }
                } else {
                    tmpRetDetail.respCode = "-1";
                    tmpRetDetail.respInfo = tmpRetStr[1];
                }
            } else {
                tmpRetDetail.respCode = "-1";
                tmpRetDetail.respInfo = "未找到账号[" + p_req.contractDetail.recAct + "]商户配置信息!";
            }
        } catch (Exception ex) {
            ex.printStackTrace();
            tmpRetDetail.respCode = "-1";
            tmpRetDetail.respInfo = ex.getMessage();
            WriteBankLogLn(ex);
        }

        retAts.writeRetDetail(tmpRetDetail);
        return retAts;
    }

    @Override
    public RetAtsDeContract qryDeContract(ReqDeContract p_reqQry) throws Exception {
        return null;
    }
}
